<?php
namespace frontend\controllers;

use backend\models\UserPost;
use common\models\Category;
use common\models\Post;
use common\models\ReportComment;
use common\models\User;
use common\models\UserCategory;
use common\models\ZancaiComment;
use common\models\ZancaiPost;
use common\WuMing;
use Yii;
use yii\web\Controller;
use yii\web\Cookie;
use common\models\Comment;
use frontend\models\Anonymous;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;


/**
 * 接收用户AJAX过来的所有数据
 * Commentcontroller
 */
class CommentController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }


    public function actionIndex($id)
    {
        $this->layout = false;
        $data = Comment::find()
            ->where(['post_id' => $id])
            ->asArray()
            ->all();
        $comment = WuMing::tree($data);
        return $this->render('index', ['comment' => $comment]);
    }

    public function actionComment()
    {

        $blackList = WuMing::isBlackList();
        if ($blackList) {
            $data['status'] = -2;
            $data['msg'] = "IP被封,距离解封还有" . $blackList;
            echo json_encode($data);
            return;
        }
        //如果用户没有登录 并且没有设置匿名信息
        if (Yii::$app->user->isGuest && !Yii::$app->request->cookies->get("anonymous_name")) {
//            $cookies = Yii::$app->response->cookies;
//            $cookies->add(new \yii\web\Cookie([
//                'name' => 'anonymous_name',
//                'value' => "无名",
//                'expire' => time() + 86400 * 365,
//            ]));
        }
        if (Yii::$app->request->isAjax) {
            $data = Yii::$app->request->post("CommentForm");
            $content = $data['content'];
            $post_id = $data['post_id'];

            $post = Post::findOne($post_id);
            $post->last_comment = time();
            $post->save();

            $comment = new Comment();
            $comment->content = $content;
            $comment->post_id = $post_id;
            $comment->status = 1;
            $comment->time = time();
            $comment->ip_address = Yii::$app->request->getUserIP();
            if (!Yii::$app->user->isGuest) {
                $user = User::findOne(Yii::$app->user->id);
                $comment->user = $user->nickname;
                $comment->user_id = $user->id;
                $comment->signature = $user->signature;
                $comment->img = $user->img;
            } else {
                if(!Yii::$app->request->cookies->has("anonymous_name")){
                    $comment->user= "无名";
                }else{
                    $comment->user = Yii::$app->request->cookies->get("anonymous_name")->value;
                }
                if(!Yii::$app->request->cookies->has("anonymous_signature")){
                    $comment->signature= "";
                }else{
                    $comment->signature = Yii::$app->request->cookies->get("anonymous_signature")->value;
                }
                $comment->img = "img/wm.jpg";
            }
            if ($comment->save()) {
                $data['status'] = 1;
                $data['msg'] = "comment success";
                echo json_encode($data);
            }
        }
    }

    public function actionReplay()
    {
        $blackList = WuMing::isBlackList(2);
        if ($blackList) {
            $data['status'] = -2;
            $data['msg'] = "IP被封,距离解封还有" . $blackList;
            echo json_encode($data);
            return;
        }
        //如果用户没有登录 并且没有设置匿名信息
        if (Yii::$app->user->isGuest && !Yii::$app->request->cookies->get("anonymous_name")) {
//            $cookies = Yii::$app->response->cookies;
//            $cookies->add(new \yii\web\Cookie([
//                'name' => 'anonymous_name',
//                'value' => "无名",
//                'expire' => time() + 86400 * 365,
//            ]));
        }
        if (Yii::$app->request->isAjax) {
            $pid = Yii::$app->request->post("pid");
            $content = Yii::$app->request->post("content");;
            $post_id = Yii::$app->request->post("post_id");
            $reply_to = Yii::$app->request->post("reply_to");

            $post = Post::findOne($post_id);
            $post->last_comment = time();
            $post->save();
            $comment = new Comment();
            $comment->content = htmlspecialchars($content);
            $comment->post_id = $post_id;
            $comment->status = 1;
            $comment->time = time();
            $comment->ip_address = Yii::$app->request->getUserIP();
            if (!Yii::$app->user->isGuest) {
                $user = User::findOne(Yii::$app->user->id);
                $comment->user = $user->nickname;
                $comment->user_id = $user->id;
                $comment->signature = $user->signature;
                $comment->img = $user->img;
                $comment->pid = $pid;
                $comment->reply_to = $reply_to;
            } else {
                if(!Yii::$app->request->cookies->has("anonymous_name")){
                    $comment->user= "无名";
                }else{
                    $comment->user = Yii::$app->request->cookies->get("anonymous_name")->value;
                }
                if(!Yii::$app->request->cookies->has("anonymous_signature")){
                    $comment->signature= "";
                }else{
                    $comment->signature = Yii::$app->request->cookies->get("anonymous_signature")->value;
                }
                $comment->img = "img/wm.jpg";
                $comment->pid = $pid;
                $comment->reply_to = $reply_to;

            }
            if ($comment->save()) {
                $data['status'] = 1;
                $data['msg'] = "comment success";
                echo json_encode($data);
            }
        }
    }

    public function actionZancai()
    {

        //    print_r(Yii::$app->request->post());die;
        if (Yii::$app->request->isAjax) {
            //获取到的参数
            $comment_id = Yii::$app->request->post("id");
            $type = Yii::$app->request->post("type");
            $ip = Yii::$app->request->getUserIP();
            //查询当前IP是否已经对这篇文章点过赞或者踩
            $zancai = ZancaiComment::findOne([
                'ip_address' => $ip,
                'comment_id' => $comment_id,
            ]);

            //查询post
            $comment = Comment::findOne($comment_id);
            $zancaicomment = new ZancaiComment();

            if ($zancai) {
                if ($zancai->type == 0) {
                    if($type == 0){
                        $data['status'] = -1;
                        $data['msg'] = "已点过赞";
                    }else{
                        $zancai->delete();
                        $comment->zan -= 1;
                        $comment->cai += 1;
                        $comment->save();

                        $zancaicomment->ip_address = $ip;
                        $zancaicomment->comment_id = $comment_id;
                        $zancaicomment->type = 1;
                        $zancaicomment->save();
                        $data['status'] = 3;
                        $data['msg'] = "取消赞,点踩成功";
                    }

                }
                if ($zancai->type == 1) {
                    if($type == 1){
                        $data['status'] = -1;
                        $data['msg'] = "已点过踩";
                    }else{
                        $zancai->delete();
                        $comment->zan += 1;
                        $comment->cai -= 1;
                        $comment->save();

                        $zancaicomment->ip_address = $ip;
                        $zancaicomment->comment_id = $comment_id;
                        $zancaicomment->type = 0;
                        $zancaicomment->save();
                        $data['status'] = 4;
                        $data['msg'] = "取消踩,点赞成功";
                    }
                }
                echo json_encode($data);
                exit;
            }
            //如果查询不到
            if (!$comment) {
                $data['status'] = -1;
                $data['msg'] = "Error!评论不存在";
                echo json_encode($data);

            } else {//如果查询到这个post
                if ($type == 0) {//赞
                    $data['status'] = 1;
                    $data['msg'] = "点赞成功";
                    echo json_encode($data);
                    $comment->zan += 1;
                    $comment->sum += 1;
                    $comment->save();
                    $zancaicomment->ip_address = $ip;
                    $zancaicomment->comment_id = $comment_id;
                    $zancaicomment->type = 0;
                    $zancaicomment->save();
                } elseif ($type == 1) { //踩
                    $data['status'] = 2;
                    $data['msg'] = "点踩成功";
                    echo json_encode($data);
                    $comment->cai += 1;
                    $comment->sum -= 1;
                    $comment->save();
                    $zancaicomment->ip_address = $ip;
                    $zancaicomment->comment_id = $comment_id;
                    $zancaicomment->type = 1;
                    $zancaicomment->save();
                } else { //如果恶意修改了type
                    $data['status'] = -1;
                    $data['msg'] = "Error!参数错误!";
                    echo json_encode($data);
                }
            }
        }

    }

    public function actionReport()
    {
        //   var_dump(Yii::$app->request->post());die;
        $ReportForm = Yii::$app->request->post("ReportForm");
        $report = new ReportComment();
        $result = $report->findOne(['ip_address' => Yii::$app->request->getUserIP(), 'comment_id' => $ReportForm['id']]);
        if ($result) {
            $data['status'] = 0;
            $data['msg'] = "举报失败，你已经举报过";
            echo json_encode($data);
        } else {
            $report->comment_id = $ReportForm['id'];
            $report->reason = $ReportForm['reason'];
            $report->ip_address = Yii::$app->request->getUserIP();
            if ($report->save()) {
                $data['status'] = 1;
                $data['msg'] = "举报成功";
                echo json_encode($data);
            }
        }


    }


    public function actionDelete()
    {
        if (Yii::$app->request->isAjax) {
            $id = Yii::$app->request->post("id");
            $comment = Comment::findOne([$id]);
            $cate_id = WuMing::getCateIdByPostId($comment->post_id);
            $post = $this->findModel($id);
            if (Yii::$app->user->can('admin_login') || WuMing::isManager(Yii::$app->user->id, $cate_id)) {
                if ($post->delete()) {
                    $data['status'] = 1;
                    $data['msg'] = "删除成功";
                    echo json_encode($data);
                } else {
                    $data['status'] = -1;
                    $data['msg'] = "删除失败";
                    echo json_encode($data);
                }
            }
        }

    }


    protected function findModel($id)
    {
        if (($model = Comment::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
